package cn.lyjuan.archetypes.bms.web.config;

import cn.lyjuan.archetypes.bms.web.util.AppUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.springframework.security.web.header.HeaderWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CharsetInterceptor implements HeaderWriter
{
    private static Logger log = LogManager.getLogger(CharsetInterceptor.class);

    @Override
    public void writeHeaders(HttpServletRequest request, HttpServletResponse response)
    {

    }

    @After("execution(* cn.lyjuan.archetypes.bms.web.controller.*.*(..))")
    public void setRespContextType(JoinPoint jp)
    {
        String ori = AppUtils.req().getHeader("content-type");
        if (log.isDebugEnabled())
            log.debug("response origin content-type: " + ori);

        AppUtils.res().setCharacterEncoding("UTF-8");
        AppUtils.res().setContentType("application/json;charset=UTF-8");

        if (log.isDebugEnabled())
            log.debug("set response context-type: application/json;charset=UTF-8");
    }
}
